package com.yx.xinruitu.controller;

import com.yx.xinruitu.controller.base.BaseController;
import com.yx.xinruitu.service.IQuestionService;
import com.yx.xinruitu.service.IStaffService;
import com.yx.xinruitu.util.ParameterMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController extends BaseController {

    @Autowired
    private IQuestionService questionService;

    @Autowired
    private IStaffService staffService;

    /**
     * 查询部门
     *
     * @return
     */
    @RequestMapping(value = "/getDept", method = RequestMethod.GET)
    @ResponseBody
    public Object getDept() {
        return questionService.getDept();
    }

    /**
     * 保存答题人员
     */
    @RequestMapping(value = "/saveStaff", method = RequestMethod.POST)
    @ResponseBody
    public Object saveStaff() {
        return staffService.add(this.getParameterMap());
    }

    /**
     * 验证答题人员
     */
    @RequestMapping(value = "/checkStaff", method = RequestMethod.POST)
    @ResponseBody
    public Object checkStaff() {
        return staffService.find(this.getParameterMap(), this.getSession());
    }

    /**
     * 开始答题
     * @return
     */
    @RequestMapping(value = "/startExam/{type}", method = RequestMethod.GET)
    public Object startExam(Model model, @PathVariable String type) {
    	String qsType=type;
    	if (type.equals("0")){
    	    long firstEndTime = 1621785599000L;
            long time = new Date().getTime();
            qsType="1";
            if (time>firstEndTime){
                qsType="2";
            }
		}
        ParameterMap parameterMap = this.getParameterMap();
        parameterMap.put("type", qsType);
        List<ParameterMap> singleQuestions = questionService.getSingleQuestions(parameterMap);
        List<ParameterMap> multipleQuestions = questionService.getMultipleQuestions(parameterMap);
        List<ParameterMap> list = new ArrayList<>();
        list.addAll(singleQuestions);
        list.addAll(multipleQuestions);
        model.addAttribute("questions", list);
        this.getSession().setMaxInactiveInterval(80*60);
        this.getSession().setAttribute("qlist", list);
        this.getSession().setAttribute("answerType", type);
		if (type.equals("0")){
			return "/page/exam/mockAnswer";
		}
        return "/page/exam/exampage";
    }

    /**
     * 保存答题结果
     */
    @RequestMapping(value = "/examCommit/{answerType}", method = RequestMethod.POST)
    @ResponseBody
    public Object examCommit(@PathVariable String answerType) {
        List<ParameterMap> list = (List<ParameterMap>) this.getSession().getAttribute("qlist");
        ParameterMap pm = this.getParameterMap();
        String res = (String) pm.get("res");

        String[] str = res.split(",");
        HashMap<Object, String> errorMap = new HashMap<>();
        double score = 0;
        for (String s : str) {
            if (s != null && !s.equals("")) {
                if (s.contains("-")) {
                    String[] val = s.split("-");
                    if (val[0] != null && !val[0].equals("") && !val[0].equals("")) {
                        
                        Object answer = list.get(Integer.parseInt(val[1])).get("answer");
                        
                        if(val[0].equals(answer)){
    
                            score += 0.5;
                        }
                    } else {
                        errorMap.put(list.get(Integer.parseInt(val[1])), val[0]);
                    }
                } else if (s.contains("=")) {
                    String[] val = s.split("=");
                    if (val[0] != null && !val[0].equals("") && val[0].equals(list.get(Integer.parseInt(val[1])).get("answer"))) {
                            score += 1;
                    } else {
                        errorMap.put(list.get(Integer.parseInt(val[1])), val[0]);
                    }
                }
            }
        }
        pm.put("score", score);
        pm.put("answerType", answerType);
        HashMap<String, Object> map;
        map = staffService.examCommit(pm);
        this.getSession().setMaxInactiveInterval(80*60);

        this.getSession().setAttribute("examres", map);
        this.getSession().setAttribute("errorMap", errorMap);
        this.getSession().setAttribute("answerType", answerType);
        return map;
    }
	/*
	  跳转结果页面
	 */
    @RequestMapping(value = "/toResult", method = RequestMethod.GET)
    public Object examCommit(Model model) {
        model.addAttribute("result", this.getSession().getAttribute("examres"));
        model.addAttribute("errorMap", this.getSession().getAttribute("errorMap"));
        return "/page/exam/result";
    }
}
